package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.common.utils.EsbUtil;
import com.ruoyi.system.domain.HrFkImg;
import com.ruoyi.system.domain.HrFkJl;
import com.ruoyi.system.domain.HrFkXdwp;
import com.ruoyi.system.domain.LhWz;
import com.ruoyi.system.mapper.HrFkImgMapper;
import com.ruoyi.system.mapper.HrFkJlMapper;
import com.ruoyi.system.mapper.HrFkXdwpMapper;
import com.ruoyi.system.service.ILhWzService;
import com.ruoyi.system.service.SendMsgService;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SendMsgServiceImpl implements SendMsgService
{

    private static final Logger log = LoggerFactory.getLogger(SendMsgServiceImpl.class);

    @Resource
    private HrFkJlMapper hrFkJlMapper;

    @Resource
    private HrFkImgMapper hrFkImgMapper;

    @Resource
    private HrFkXdwpMapper hrFkXdwpMapper;

    @Autowired
    private ILhWzService lhWzService;

    private static final String SUCCESS_CODE = "100";

    private static final String ERROR_CODE = "303";

    private static final String SUCCESS = "ok";

    private static final String ERROR = "err";

    private static final Long SUCCESS_BJ = 1L;

    private static final String XDWP = "xdwp";

    @Override
    public void sendMessage() {
        List<HrFkJl> data = hrFkJlMapper.selectHrFkJlListByNotSX();
        for (HrFkJl d : data) {
            try {
                Map<String, String>  hrFkImgMap = new HashMap<>();
                Map<String, String> xdwpMap = new HashMap<>();
                List<HrFkImg> hrFkImgs = hrFkImgMapper.selectHrFkImgList(new HrFkImg(d.getStrid()));
                if (!CollectionUtils.isEmpty(hrFkImgs)) {
                    hrFkImgMap  = hrFkImgs.stream().filter(h -> !h.getLxmc().equals(XDWP)).collect(Collectors.toMap(HrFkImg::getLxmc, HrFkImg::getFilename));
                    xdwpMap = hrFkImgs.stream().filter(h -> h.getLxmc().equals(XDWP)).collect(Collectors.toMap(HrFkImg::getXdwpxh, HrFkImg::getFilename));
                }
                JSONObject object = new JSONObject();
                object.put("requestName","车辆".equals(d.getFkLx()) ? "车辆预约" : "访客预约");  //流程标题
                //object.put("workflowId","车辆".equals(d.getFkLx()) ? "92522" : "92521");  //测试流程id
                object.put("workflowId","车辆".equals(d.getFkLx()) ? "104522" : "104521");  //正式流程id
                object.put("mainData",getMainData(d, hrFkImgMap));  //主表json
                if ("车辆".equals(d.getFkLx())) {
                    List<HrFkJl> accompanying = hrFkJlMapper.selectHrFkJlList(new HrFkJl(d.getStrid()));
                    object.put("detailData", getDetailData(d, xdwpMap, accompanying));
                }
                object.put("remark","测试");//发起节点意见
                object.put("usercode", d.getFkSfygbh()); //创建人编号
                object.put("username", d.getFkSfygxm()); //创建人姓名
                log.info("请求参数：" + object.toJSONString());
                String result = EsbUtil.getService(object);
                log.info("请求结果：" + result);
                if (StringUtils.isNotBlank(result)) {
                    JSONObject resultObject = JSONObject.parseObject(result);
                    if (StringUtils.equals(resultObject.getString("code"), SUCCESS_CODE)) {
                        hrFkJlMapper.updateHrFkJl(new HrFkJl(d.getAutoid(), resultObject.getJSONObject("data").getString("requestid"), SUCCESS, SUCCESS_BJ));
                    } else if (StringUtils.equals(resultObject.getString("code"), ERROR_CODE)) {
                        hrFkJlMapper.updateHrFkJl(new HrFkJl(d.getAutoid(), ERROR, resultObject.getString("msg")));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.info("发生异常：" + e.getLocalizedMessage() + "，异常数据：" + JSONObject.toJSONString(d));
                hrFkJlMapper.updateHrFkJl(new HrFkJl(d.getAutoid(), ERROR, "发生异常：" + e.getLocalizedMessage()));
            }

        }
    }

    public String getDetailData(HrFkJl data, Map<String, String> xdwpMap, List<HrFkJl> accompanying) {
        JSONArray result = new JSONArray();
        if (xdwpMap.size() > 0) {
            JSONObject table1 = new JSONObject();
            JSONArray WorkflowRequestTableRecords = new JSONArray();
            table1.put("tableDBName", "formtable_main_681_dt1");
            for (String key : xdwpMap.keySet()) {
                HrFkXdwp xdwp = hrFkXdwpMapper.selectByImgstr(key);
                JSONObject d = new JSONObject();
                d.put("recordOrder", 0);
                JSONArray workflowRequestTableFields = new JSONArray();
                JSONObject wpmc = new JSONObject();
                wpmc.put("fieldName", "wpmc");
                wpmc.put("fieldValue", xdwp.getWpmc());
                workflowRequestTableFields.add(wpmc);

                JSONObject sl = new JSONObject();
                sl.put("fieldName", "sl");
                sl.put("fieldValue", xdwp.getWpsl());
                workflowRequestTableFields.add(sl);

                JSONObject ggpp = new JSONObject();
                ggpp.put("fieldName", "ggpp");
                ggpp.put("fieldValue", xdwp.getWpgg());
                workflowRequestTableFields.add(ggpp);

                JSONObject zp = new JSONObject();
                zp.put("fieldName", "zp");
                zp.put("fieldValue", xdwpMap.get(key));
                workflowRequestTableFields.add(zp);

                d.put("workflowRequestTableFields", workflowRequestTableFields);
                WorkflowRequestTableRecords.add(d);
            }
            table1.put("WorkflowRequestTableRecords", WorkflowRequestTableRecords);
            result.add(table1);
        }
        if (accompanying.size() > 0) {
            JSONObject table2 = new JSONObject();
            JSONArray WorkflowRequestTableRecords = new JSONArray();
            table2.put("tableDBName", "formtable_main_681_dt2");
            accompanying.forEach(d -> {
                List<HrFkImg> hrFkImgs = hrFkImgMapper.selectHrFkImgList(new HrFkImg(d.getStrid()));
                Map<String, String> hrFkImgMap  = hrFkImgs.stream().filter(h -> !h.getLxmc().equals(XDWP)).collect(Collectors.toMap(HrFkImg::getLxmc, HrFkImg::getFilename));

                JSONObject sx = new JSONObject();
                sx.put("recordOrder", 0);
                JSONArray workflowRequestTableFields = new JSONArray();

                JSONObject xm = new JSONObject();
                xm.put("fieldName", "xm");
                xm.put("fieldValue", d.getFkXm());
                workflowRequestTableFields.add(xm);

                JSONObject drxcm = new JSONObject();
                drxcm.put("fieldName", "drxcm");
                drxcm.put("fieldValue", MapUtils.getString(hrFkImgMap, "xcm", ""));
                workflowRequestTableFields.add(drxcm);

                JSONObject jkm = new JSONObject();
                jkm.put("fieldName", "jkm");
                jkm.put("fieldValue", MapUtils.getString(hrFkImgMap, "jkm", ""));
                workflowRequestTableFields.add(jkm);

                JSONObject hszm = new JSONObject();
                hszm.put("fieldName", "hszm");
                hszm.put("fieldValue", MapUtils.getString(hrFkImgMap, "hsbg", ""));
                workflowRequestTableFields.add(hszm);

                JSONObject dh = new JSONObject();
                dh.put("fieldName", "dh");
                dh.put("fieldValue", d.getFkLxfs());
                workflowRequestTableFields.add(dh);

                sx.put("workflowRequestTableFields", workflowRequestTableFields);
                WorkflowRequestTableRecords.add(sx);
            });
            table2.put("WorkflowRequestTableRecords", WorkflowRequestTableRecords);
            result.add(table2);
        }
        return result.toJSONString();
    }

    private String getMainData(HrFkJl data, Map<String, String> img) {
        JSONArray array = new JSONArray();

        JSONObject xm = new JSONObject();
        xm.put("fieldName", "xm");
        xm.put("fieldValue", data.getFkXm());
        array.add(xm);

        JSONObject dw = new JSONObject();
        dw.put("fieldName", "dw");
        dw.put("fieldValue", data.getFkDw());
        array.add(dw);

        JSONObject lxfs = new JSONObject();
        lxfs.put("fieldName", "lxfs");
        lxfs.put("fieldValue", data.getFkLxfs());
        array.add(lxfs);

        JSONObject sqrq = new JSONObject();
        sqrq.put("fieldName", "sqrq");
        sqrq.put("fieldValue", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, data.getFkYyrq()));
        array.add(sqrq);

        JSONObject lfsy = new JSONObject();
        lfsy.put("fieldName", "lfsy");
        lfsy.put("fieldValue", data.getFkLfsy());
        array.add(lfsy);

        JSONObject dfdw = new JSONObject();
        dfdw.put("fieldName", "dfdw");
        dfdw.put("fieldValue", data.getFkDfdw());
        array.add(dfdw);

        JSONObject sfygbh = new JSONObject();
        sfygbh.put("fieldName", "sfygbh");
        sfygbh.put("fieldValue", data.getFkSfygbh());
        array.add(sfygbh);

        JSONObject jrsj = new JSONObject();
        jrsj.put("fieldName", "jrsj");
        jrsj.put("fieldValue", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, data.getFkJrrq()));
        array.add(jrsj);

        JSONObject lksj = new JSONObject();
        lksj.put("fieldName", "lksj");
        lksj.put("fieldValue", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, data.getFkLkrq()));
        array.add(lksj);

        JSONObject drxcm = new JSONObject();
        drxcm.put("fieldName", "drxcm");
        drxcm.put("fieldValue", MapUtils.getString(img, "xcm", ""));
        array.add(drxcm);

        JSONObject jkm = new JSONObject();
        jkm.put("fieldName", "jkm");
        jkm.put("fieldValue", MapUtils.getString(img, "jkm", ""));
        array.add(jkm);

        JSONObject hsbg = new JSONObject();
        hsbg.put("fieldName", "hsbg");
        hsbg.put("fieldValue", MapUtils.getString(img, "hsbg", ""));
        array.add(hsbg);

        JSONObject dfjd = new JSONObject();
        dfjd.put("fieldName", "dfjd");
        dfjd.put("fieldValue", data.getFkDfjd());
        array.add(dfjd);

        if ("车辆".equals(data.getFkLx())) {
            JSONObject sfygxmwz = new JSONObject();
            sfygxmwz.put("fieldName", "sfygxmwz");
            sfygxmwz.put("fieldValue", data.getFkSfygxm());
            array.add(sfygxmwz);

            JSONObject cph = new JSONObject();
            cph.put("fieldName", "cph");
            cph.put("fieldValue", data.getFkCph());
            array.add(cph);

            JSONObject xsz = new JSONObject();
            xsz.put("fieldName", "xsz");
            xsz.put("fieldValue", MapUtils.getString(img, "xsz", ""));
            array.add(xsz);

            JSONObject jsz = new JSONObject();
            jsz.put("fieldName", "jsz");
            jsz.put("fieldValue", MapUtils.getString(img, "jsz", ""));
            array.add(jsz);

            JSONObject txmg = new JSONObject();
            txmg.put("fieldName", "txmg");
            txmg.put("fieldValue", StringUtils.nvl(data.getFkTxmg(), ""));
            array.add(txmg);

            JSONObject sfgb = new JSONObject();
            sfgb.put("fieldName", "sfgb");
            sfgb.put("fieldValue", StringUtils.nvl(data.getFkSfgb(), ""));
            array.add(sfgb);

            JSONObject gbwz = new JSONObject();
            gbwz.put("fieldName", "gbwz");
            gbwz.put("fieldValue", data.getFkGbwz());
            array.add(gbwz);

            JSONObject sfxdwp = new JSONObject();
            sfxdwp.put("fieldName", "sfxdwp");
            sfxdwp.put("fieldValue", data.getFkSfxdwp());
            array.add(sfxdwp);

            JSONObject cllx = new JSONObject();
            cllx.put("fieldName", "cllx");
            cllx.put("fieldValue", data.getFkCllx());
            array.add(cllx);

            JSONObject txcmg = new JSONObject();
            txcmg.put("fieldName", "txcmg");
            txcmg.put("fieldValue", data.getFkTxmg());
            array.add(txcmg);

            JSONObject dfsy = new JSONObject();
            dfsy.put("fieldName", "dfsy");
            dfsy.put("fieldValue", data.getFkLfsy());
            array.add(dfsy);

            JSONObject cgsprbh = new JSONObject();
            cgsprbh.put("fieldName", "cgsprbh");
            cgsprbh.put("fieldValue", data.getCgsprbh());
            array.add(cgsprbh);

        } else {
            JSONObject sfygxmhz = new JSONObject();
            sfygxmhz.put("fieldName", "sfygxmhz");
            sfygxmhz.put("fieldValue", data.getFkSfygxm());
            array.add(sfygxmhz);
        }

        return array.toJSONString();
    }
}
